﻿Imports Mystery
Imports System.Runtime.CompilerServices

''' <summary>
''' called by mistery to start with
''' </summary>
''' <remarks></remarks>
<GlobalAvalibleObject()>
Public Interface INewWindow

    Function getTitle() As String

End Interface

Public Module ControlExtension
    <Extension()>
    Public Function PopupDialog(c As UserControl) As Boolean?
        Dim new_window As New Window
        Dim mystery_new_window As INewWindow = new_window.getGlobalObject(Of INewWindow)()
        new_window.Title = mystery_new_window.getTitle

        new_window.Height = c.Height + 20
        new_window.Width = c.Width + 20
        new_window.SizeToContent = SizeToContent.WidthAndHeight
        new_window.WindowStartupLocation = WindowStartupLocation.CenterScreen
        new_window.Content = c
        Return new_window.ShowDialog()
    End Function

    <Extension()>
    Public Function instanceWindow(c As UserControl) As Window
        Dim new_window As New Window
        Dim mystery_new_window As INewWindow = new_window.getGlobalObject(Of INewWindow)()
        new_window.Content = c
        new_window.Title = mystery_new_window.getTitle()
        new_window.Height = c.Height + 20
        new_window.Width = c.Width + 20
        new_window.SizeToContent = SizeToContent.WidthAndHeight
        new_window.WindowStartupLocation = WindowStartupLocation.CenterScreen
        Return new_window
    End Function

    <Extension()>
    Public Function createImage(Control As UserControl) As BitmapImage
        Dim bmp As RenderTargetBitmap = New RenderTargetBitmap(Control.ActualWidth, Control.ActualHeight, 96D, 96D, PixelFormats.Pbgra32)
        bmp.Render(Control)
        Dim enc As PngBitmapEncoder = New PngBitmapEncoder()
        enc.Frames.Add(BitmapFrame.Create(bmp))

        Dim stream As New System.IO.MemoryStream()
        enc.Save(stream)
        Dim imageSource As New BitmapImage() With {.CacheOption = BitmapCacheOption.OnLoad}
        imageSource.BeginInit()
        imageSource.StreamSource = stream
        imageSource.EndInit()
        Return imageSource
    End Function

End Module

<GlobalAvalibleObjectImplementation(implementation_of:=GetType(INewWindow), overrides_exsisting:=False, singleton:=True)>
Public Class DefaultNewWindow
    Implements INewWindow

    Public Function getTitle() As String Implements INewWindow.getTitle
        Return "Mistery"
    End Function

End Class
